package com.lemon.lemonai.controller;

import com.lemon.lemonai.constant.TypeEnum;
import com.lemon.lemonai.repository.ChatHistoryRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY;

/**
 * @Description: AI智能客服
 * @Author: lemon
 * @CreateTime: 2025-03-23  11:01
 * @Version: 1.0
 */
@RestController
@RequestMapping("/ai")
@RequiredArgsConstructor
public class ServiceChatController {

    private final ChatClient serviceChatClient;

    private final ChatHistoryRepository chatHistoryRepository;

    @RequestMapping(value = "/service", produces = "text/html;charset=utf-8")
    public Flux<String> chat(String prompt, String chatId){
        chatHistoryRepository.save(TypeEnum.SERVICE.getValue(), chatId);
        return serviceChatClient.prompt()
                .user(prompt)
                .advisors(advisorSpec -> advisorSpec.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId))
                .stream()
                .content();
    }
}
